package com.liy.cloud.auth.client.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/*
*  authentication:  认证 ( 是谁 ?)  authentic: adj: 可信的,真正的
*
*
*  authorization:   授权 ( 有什么权限? 给什么权限  )   authorize:  vt. 批准, 授权给
*
*
*  授权拦截器配置
*/
@Configuration
public class AuthorizationInterceptorConfig implements WebMvcConfigurer {

    private static Logger log = LoggerFactory.getLogger(AuthorizationInterceptorConfig.class);

    @Value("${dascloud.authrization.enabled: true}")
    private Boolean enabled;

    @Value("${dascloud.authrization.patterens: /**}")
    private String patterns;

    @Value("${dascloud.authrization.excludePatterns: /info,/api/*,/error*}")
    private String excludePatterns = "";


    @Autowired
    private AuthorizationInterceptorService serviceAuthorizationInterceptor;


    /**
    *  add interceptor
    **/
    public void addInterceptors(InterceptorRegistry registry) {

        if (this.enabled) {
            log.info("Added Authentication Interceptor Successfully.");

            InterceptorRegistration interceptorRegistration = registry.addInterceptor(this.serviceAuthorizationInterceptor);

            String[] patternArr = this.patterns.split(",");// 拦截
            interceptorRegistration.addPathPatterns(patternArr);

            String[] excludePatternArr = this.excludePatterns.split(",");// 排除
            interceptorRegistration.excludePathPatterns(excludePatternArr);

            WebMvcConfigurer.super.addInterceptors(registry);
        }
    }
}
